﻿using GBH.Models.Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace GBH.Models
{
    public class CallPIPerProductPerLocation
    {
        private decimal _daySell;
        public DateTime RollingStart { get; set; }
        public int RollingOutDay { get; set; }
        public List<Models.sell_out> Sellouts { get; set; }
        public stock Stock { get; set; }
        public int PI { get; set; }
        public daysellMinMax DaySellMinMax { get; set; }
        public decimal QtyDelay { get; set; }

        // Min ROP Max its a day need to plus with _daysell before using
        public int MinStock { get; set; }
        public int ROP { get; set; }
        public int MaxStock { get; set; }
        public int ConverUnit { get; set; }

        public CallPIPerProductPerLocation() { }
        public CallPIPerProductPerLocation(daysellMinMax daySellMinMax)
        {
            MinStock = daySellMinMax.min ?? 0;
            ROP = daySellMinMax.rop ?? 0;
            MaxStock = daySellMinMax.max ?? 0;
            DaySellMinMax = daySellMinMax;
        }

        public int CalPi()
        {
            DateTime dateTo = RollingStart;
            DateTime dateFrom = RollingStart.AddDays(RollingOutDay * -1);

            foreach (var s in Sellouts)
            {
                if (s.date_time.Value.Year > 2500)
                {
                    s.date_time = s.date_time.Value.AddYears(-543);
                }
            }
            var daysell = from sell in Sellouts
                          //where sell.date_time <= dateTo.AddDays(1) && sell.date_time >= dateFrom
                          select sell.qty;
            int daysellSum = daysell.Sum() ?? 0;
            _daySell = Math.Round(decimal.Divide(daysellSum, RollingOutDay), 2);

            //Todo need to minus stock balance too.
            if (DaySellMinMax.location_id == "BP" && DaySellMinMax.product_id == "8858721510672")
            {
                var k = "debug";
            }
            int max = DaySellMinMax.max ?? 0;
            int qtyStock = Stock.qty ?? 0;
            var RopSell = DaySellMinMax.rop * _daySell;
            var MaxSell = DaySellMinMax.max * _daySell;
            int pi = 0;
            if (qtyStock + (int)QtyDelay < RopSell)
            {
                pi = Convert.ToInt32((max * _daySell)) - qtyStock - (int)QtyDelay;
            }

            if (ConverUnit != 0)
            {
                pi = pi % ConverUnit != 0 ? ((pi / ConverUnit) + 1) * ConverUnit : pi;
                //add logic to make sure minimum of stock are equal to 1 box/set
                if ((pi <= ConverUnit) && (qtyStock + (int)QtyDelay <= ConverUnit))
                {
                    pi = ConverUnit;
                }
            }
            // if current stock more than daily sell no need to order new

            if (pi < 0)
            {
                pi = 0;
            }

            PI = pi;
            return PI;
        }






    }
}